from django.db import models
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import Group


class SmartPage(models.Model):
    """
    A Flatpage-like that includes restriction by group and reversable URL (using smartpage_url templatetag)
    """
    url = models.CharField(_('URL'), max_length=100, db_index=True)
    title = models.CharField(_('title'), max_length=200)
    slug = models.SlugField(help_text=_('This will be used in the reverse method'))
    content = models.TextField(_('content'), blank=True)
    enable_comments = models.BooleanField(_('enable comments'))
    template_name = models.CharField(_('template name'), max_length=70, blank=True,
        help_text=_("Example: 'smartpages/contact_page.html'. If this isn't provided, the system will use 'smartpages/default.html'."))
    restricted = models.BooleanField(_('restriction required'), help_text=_("If this is checked, only users in the following group will be able to view the page."))
    #TODO: implement a restriction by user level (logged in, superuser, etc)
    sites = models.ManyToManyField(Site)
    group = models.ForeignKey(Group, blank=True, null=True)

    class Meta:
        verbose_name = _('smart page')
        verbose_name_plural = _('smart pages')
        ordering = ('url',)

    def __unicode__(self):
        return u"%s -- %s" % (self.url, self.title)

    def get_absolute_url(self):
        return self.url
